package practice.sort.compare;

import practice.sort.Sort;

/**
 * @Description:
 * @Author Ammar
 * @Create 2023/8/10 13:43
 */
public class RadixSort extends Sort {
    @Override
    public void sort() {
        int max = nums[0];
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > max) max = nums[i];
        }

        for (int i = 1; i <= max; i *= 10) {
            sort(i);
        }
    }

    public void sort(int divider) {
        int[] counts = new int[10];
        for (int i = 0; i < nums.length; i++) {
            counts[nums[i] / divider % 10]++;
        }
        for (int i = 1; i < counts.length; i++) {
            counts[i] += counts[i - 1];
        }
        int[] newNums = new int[nums.length];
        for (int i = nums.length - 1; i >= 0; i--) {
            newNums[--counts[nums[i] / divider % 10]] = nums[i];
        }
        for (int i = 0; i < nums.length; i++) {
            nums[i] = newNums[i];
        }
    }
}
